<template>
  <view class="sheet" @click.stop="cancel" v-if="value">
    <view @click.stop>
      <text>未登录</text>
      <text>登录后体验完整功能哦</text>
      <view @click.stop>
        <button @click="cancel">取消</button>
        <button open-type="getUserInfo" @getuserinfo="getUserInfo">
          授权登录
        </button>
      </view>
    </view>
  </view>
</template>
<script>
export default {
  props: {
    value: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      //   authModel: true,
    };
  },
  methods: {
    cancel() {
      this.$emit("input", false);
    },
    login() {
      return new Promise((reslove, reject) => {
        uni.login({
          success: (res) => {
            reslove(res);
          },
        });
      });
    },
    async openAuth() {
      let { code } = await this.login();
      uni.getUserInfo({
        success: (res) => {
          console.log(res);
          if (res.errMsg == "getUserInfo:ok") {
            try {
              const { signature, encryptedData, iv, userInfo } = res;
              this.$http(
                {
                  url: "/passport/user/ciff/login/weixin",
                  method: "POST",
                  urlType: true,
                },
                {
                  code,
                  signature,
                  userEncryptedData: encryptedData,
                  iv,
                },
                true
              ).then((rs) => {
                if (rs[1].data.code === "000000") {
                  console.log(rs[1].data.data);
                  //   this.userInfo = rs[1].data.data;
                  uni.setStorageSync("userInfo", rs[1].data.data);
                  uni.showToast({
                    title: "登录成功",
                    icon: "none",
                    duration: 1000,
                  });
                  this.cancel();
                  //登录成功后重新刷新所有数据
                  const pages = getCurrentPages();
                  console.log(pages);
                  const perpage = pages[pages.length - 1];
                  perpage.onLoad();
                  perpage.onShow();
                  perpage.onReady();
                }
              });
            } catch (error) {}
          }
        },
        fail: (res) => {
          uni.showToast({
            title: "未授权用户信息",
            icon: "none",
            duration: 1000,
          });
          this.cancel();
        },
      });
    },
    getUserInfo(e) {
      // if (uni.getStorageSync('userInfo')) return //已经有登录过
      this.openAuth();
    },
  },
};
</script>
<style lang="scss" scoped>
.sheet {
  width: 100vw;
  height: 80vh;
  position: fixed;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 99999999;
  //   background-color: ;
  > view {
    width: 70%;
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 99999999999999;
    background-color: #ffffff;
    height: 300rpx;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    border-radius: 10rpx;
    font-size: 30rpx;
    border: 1px solid #eee;
    > view {
      display: flex;
      height: 77rpx;
      button {
        flex: 1;
        background-color: #ffffff;
        font-size: 26rpx;
        border-top: 1px solid #eee;
        border-radius: 0;
        height: 100%;
        line-height: 77rpx;
        &:first-child {
          border-right: 1px solid #eee;
        }
        &:last-child {
          font-weight: 600;
        }
        &::after {
          border: none !important;
          border-radius: 0;
        }
      }
    }
    > text {
      font-size: 26rpx;
      width: 100%;
      text-align: center;
      &:first-child {
        padding-top: 40rpx;
        font-size: 30rpx;
        font-weight: 600;
      }
    }
  }
}
</style>
